package com.springboot2.controller;

import com.springboot2.entity.Banner;
import com.springboot2.service.BannerService;
import com.springboot2.utils.Result;
import jdk.nashorn.internal.runtime.regexp.joni.ast.BackRefNode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("banner/")
public class BannerController {

    @Resource
    private BannerService bannerService;
    //增
    @PostMapping("add")
    public Result add(@RequestBody Banner banner){
        bannerService.add(banner);
        return Result.success();
    }
    //删
    @DeleteMapping("del/{id}")
    public Result del(@PathVariable int id){
        bannerService.del(id);
        return Result.success();
    }
    //改
    @PutMapping("update")
    public Result update (@RequestBody Banner banner){
        bannerService.update(banner);
        return Result.success();
    }
    //获取对象
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable int id){
        return Result.success(bannerService.findById(id));
    }
    //分页列表
    @GetMapping("findByPage/{pageNo}/{pageSize}")
    public Result findByPage(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",bannerService.findByPage(pageNo, pageSize));
        map.put("total",bannerService.count());
        return Result.success(map);
    }
    //查询条件
    @GetMapping("findByPageParams/{pageNo}/{pageSize}/{title}")
    public Result findByPageParams(@PathVariable int pageNo,@PathVariable int pageSize,@PathVariable String title){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",bannerService.findByPageParams(pageNo, pageSize, title));
        map.put("total",bannerService.countParams());
        return Result.success(map);
    }
    //全部-删除
    @DeleteMapping("batch/{ids}")
    public Result batch(@PathVariable String ids){
        String [] idsArray = ids.split(",");
        for (String id:idsArray){
            bannerService.del(Integer.parseInt(id));
        }
        return Result.success();
    }
    //app - 列表

    @GetMapping("listStatus")
    public Result listStatus(){
        return Result.success(bannerService.findByStatus());
    }
}
